package com.xinghou.platform.server.api.bussiness.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.xinghou.platform.util.PhotoPathUtil;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.main.PhotoDAO;
import com.xinghou.platform.dao.main.UserDAO;
import com.xinghou.platform.entity.resp.ShareInfoPhotoResp;
import com.xinghou.platform.model.main.Photo;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.server.api.bussiness.PhotoAlbumBusinessService;
import com.xinghou.platform.server.api.common.Constants;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.DateUtils;
/**
 * 
 * @author wzk
 *
 */
@Service("photoAlbumService")
public class PhotoAlbumBusinessServiceImpl implements PhotoAlbumBusinessService {
	//@Resource
	//private AlbumImageDAO albumImageDAO;
	@Resource
	private PhotoDAO photoDAO;
	@Resource
	private UserDAO userDAO;
	
	private String photouri = ConfigUtil.getProperty("file_download");

	@Override
	public List<ShareInfoPhotoResp> getPhotoAlbumList(int userId,Timestamp time,int rowNum) {
		/*List<ShareInfoPhotoResp> list = new ArrayList<ShareInfoPhotoResp>();
		List<AlbumImage> albumImages = albumImageDAO.getAlbumImageList(userId, time, rowNum);
		for(AlbumImage image : albumImages){
			ShareInfoPhotoResp shareInfoPhotoResp = new ShareInfoPhotoResp();
			shareInfoPhotoResp.setSourceid(String.valueOf(image.getParentId()));
			shareInfoPhotoResp.setSourcetype(String.valueOf(image.getType()));
			shareInfoPhotoResp.setPhotoid(String.valueOf(image.getId()));
			String imguri = image.getUri();
			int index = imguri.lastIndexOf("/");
			if(index !=-1 )
			{
				String imgname = imguri.substring(index+1);
				if(imgname.indexOf(".") != -1)
				{
					imgname = "160"+imgname;
					shareInfoPhotoResp.setSimphotourl(PhotoPathUtil.getDB160(photouri+imguri));
				}
			}
			shareInfoPhotoResp.setPhotourl(photouri+imguri);
			shareInfoPhotoResp.setCreatetime(DateUtils.getNewZhCNDateTime(image.getCreateTime()));
			list.add(shareInfoPhotoResp);
		}*/
		return null;
	}

	@Override
	public List<ShareInfoPhotoResp> getPhotos(Integer userid,String fromno) {
		List<ShareInfoPhotoResp> list = new ArrayList<ShareInfoPhotoResp>();
		List<ShareInfoPhotoResp> list2 = new ArrayList<ShareInfoPhotoResp>();
		if(null==userid)
		{
			return list;
		}
		int num = Integer.MAX_VALUE;
		if(fromno!=null && !fromno.equals("-1"))
		{
			num = Integer.parseInt(fromno);
		}
		User user = userDAO.getUserById(userid);
		int headid = -1;
		if(user != null){
			headid = user.getHeadid();
		}
		List<Photo> photolist = photoDAO.getPhotos(userid,num,Constants.HEADLIMIT);
		//ShareInfoPhotoResp[] arr = new ShareInfoPhotoResp[photolist.size()];
		for(Photo photo : photolist)
		{
			ShareInfoPhotoResp shareInfoPhotoResp = new ShareInfoPhotoResp();
			shareInfoPhotoResp.setPhotoid(photo.getId().toString());
			String photoUri = photo.getPhotoUri();
			String imgname = photo.getName()+".jpg";
			shareInfoPhotoResp.setSimphotourl(PhotoPathUtil.getDB160(photouri+photoUri));
			shareInfoPhotoResp.setPhotourl(photouri+photoUri);
			shareInfoPhotoResp.setCreatetime(photo.getCreateTime().toString());
			if(headid == photo.getId())
			{
				list2.add(shareInfoPhotoResp);
			}else{
				list.add(shareInfoPhotoResp);
			}
		}
		list2.addAll(list);
		return list2;
	}
}
